<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
  <head>
    <title>Indexed Database Property Test</title>

    <script type="text/javascript"
            src="/tests/SimpleTest/SimpleTest.js">
    </script>

    <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>

    <script type="text/javascript;version=1.7">
      function testSteps()
      {
        const databaseName = window.location.pathname;
        const databaseVersion = 1;
        const objectStoreName = "foo";
        const mutableFileName = "bar";
        const mutableFileKey = 42;

        info("opening database");

        let request = indexedDB.open(databaseName, databaseVersion);
        request.onerror = errorHandler;
        request.onupgradeneeded = grabEventAndContinueHandler;
        request.onsuccess = unexpectedSuccessHandler;
        let event = yield undefined;

        info("creating object store");

        let db = event.target.result;
        db.onerror = errorHandler;
        db.onversionchange = function(event) {
          is(event.oldVersion, databaseVersion, "got correct oldVersion");
          is(event.newVersion, null, "got correct newVersion");
          db.close();
        };

        let objectStore = db.createObjectStore(objectStoreName,
                                               { autoIncrement: true });

        request.onupgradeneeded = unexpectedSuccessHandler;
        request.onsuccess = grabEventAndContinueHandler;
        event = yield undefined;

        info("creating mutable file");

        request = db.createMutableFile(mutableFileName);
        request.onsuccess = grabEventAndContinueHandler;
        event = yield undefined;

        let mutableFile = event.target.result;

        verifyMutableFile(mutableFile, getFile(mutableFileName, "", ""));
        yield undefined;

        objectStore = db.transaction([objectStoreName], "readwrite")
                        .objectStore(objectStoreName);

        info("adding mutable file");

        request = objectStore.add(mutableFile, mutableFileKey);
        request.onsuccess = grabEventAndContinueHandler;
        event = yield undefined;

        info("getting mutable file");

        request = objectStore.get(mutableFileKey);
        request.onsuccess = grabEventAndContinueHandler;
        event = yield undefined;

        verifyMutableFile(event.target.result,
                          getFile(mutableFileName, "", ""));
        yield undefined;

        info("opening database");

        request = indexedDB.open(databaseName, databaseVersion);
        request.onerror = errorHandler;
        request.onupgradeneeded = unexpectedSuccessHandler;
        request.onsuccess = grabEventAndContinueHandler;
        event = yield undefined;

        let db2 = event.target.result;
        db2.onerror = errorHandler;
        db2.onversionchange = function(event) {
          is(event.oldVersion, databaseVersion, "got correct oldVersion");
          is(event.newVersion, null, "got correct newVersion");
          db2.close();
        };

        objectStore = db2.transaction([objectStoreName], "readwrite")
                         .objectStore(objectStoreName);

        info("adding mutable file");

        request = objectStore.add(mutableFile);
        request.onsuccess = grabEventAndContinueHandler;
        event = yield undefined;

        info("getting mutable file");

        request = objectStore.get(event.target.result);
        request.onsuccess = grabEventAndContinueHandler;
        event = yield undefined;

        verifyMutableFile(event.target.result,
                          getFile(mutableFileName, "", ""));
        yield undefined;

        info("setting file handle pref");

        SpecialPowers.pushPrefEnv({ set: [["dom.fileHandle.enabled", false]] },
                                  continueToNextStep);
        yield undefined;

        info("opening database");

        request = indexedDB.open(databaseName, databaseVersion);
        request.onerror = errorHandler;
        request.onupgradeneeded = unexpectedSuccessHandler;
        request.onsuccess = grabEventAndContinueHandler;
        event = yield undefined;

        let db3 = event.target.result;
        db3.onerror = errorHandler;
        db3.onversionchange = function(event) {
          is(event.oldVersion, databaseVersion, "got correct oldVersion");
          is(event.newVersion, null, "got correct newVersion");
          db3.close();
        };

        info("creating mutable file");

        try {
          db3.createMutableFile(mutableFileName);
          ok(false, "Should have thrown!");
        }
        catch (e) {
          ok(e instanceof DOMException, "Got exception.");
          is(e.name, "InvalidStateError", "Good error.");
          is(e.code, DOMException.INVALID_STATE_ERR, "Good error code.");
        }

        objectStore = db3.transaction([objectStoreName], "readwrite")
                         .objectStore(objectStoreName);

        info("adding mutable file");

        try {
          objectStore.add(mutableFile);
          ok(false, "Should have thrown!");
        }
        catch (e) {
          ok(e instanceof DOMException, "Got exception.");
          is(e.name, "DataCloneError", "Good error.");
          is(e.code, DOMException.DATA_CLONE_ERR, "Good error code.");
        }

        info("getting mutable file");

        request = objectStore.get(mutableFileKey);
        request.onsuccess = grabEventAndContinueHandler;
        event = yield undefined;

        try {
          let result = request.result;
          ok(false, "Should have thrown!");
        }
        catch (e) {
          ok(e instanceof DOMException, "Got exception.");
          is(e.name, "InvalidStateError", "Good error.");
          is(e.code, DOMException.INVALID_STATE_ERR, "Good error code.");
        }

        info("deleting database");

        request = indexedDB.deleteDatabase(databaseName);
        request.onerror = errorHandler;
        request.onsuccess = grabEventAndContinueHandler;

        event = yield undefined;

        info("resetting file handle pref");

        SpecialPowers.popPrefEnv(continueToNextStep);
        yield undefined;

        finishTest();
        yield undefined;
      }
    </script>

    <script type="text/javascript;version=1.7" src="helpers.js"></script>
    <script type="text/javascript;version=1.7" src="file.js"></script>

  </head>

  <body onload="runTest();"></body>

</html>
